package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UserInfoPeer extends BasePeer{
	public static final String tableName = "UserInfo";
	public static final String USERID = "userId";
	public static final String NICENAME = "nicename";
	public static final String ICON = "icon";
	public static final String DESCRIPTION = "description";
	public static final String FAX = "fax";
	public static final String WEBSITE = "website";
	public static final String OTHER = "other";
	public static final String SIGN = "sign";
	public static final String POSTSCOUNTS = "postsCounts";
	public static final String[] columnNames=new String[]{"userId","nicename","icon","description","fax","website","other","sign","postsCounts"};
	public static UserInfo getUserInfoByPKs(Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserInfoPeer.tableName,UserInfoPeer.columnNames," TRUE  AND `userId`=? ",new Object[]{userId,},0, 1,conn);
			UserInfo userInfo = new UserInfo();
		fill(rs,userInfo);
		return userInfo;
	}
	public static UserInfo getUserInfoReferenceColumnsOnlyByPKs(Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserInfoPeer.tableName,new String[]{"userId",}," TRUE AND `userId`=?",new Object[]{userId,}, 0,  1,conn);
			UserInfo userInfo = new UserInfo();
		fill(rs,userInfo);
		return userInfo;
	}
	public static UserInfo getUserInfoByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserInfoPeer.tableName,UserInfoPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			UserInfo userInfo = new UserInfo();
		fill(rs,userInfo);
		return userInfo;
	}
	public static void fill(ResultSet rs, UserInfo userInfo) throws SQLException {
		if(rs.next()){
			try{userInfo.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userInfo.setNicename(rs.getString("nicename"));}catch(Exception ex){}
			try{userInfo.setIcon(rs.getLong("icon"));}catch(Exception ex){}
			try{userInfo.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{userInfo.setFax(rs.getString("fax"));}catch(Exception ex){}
			try{userInfo.setWebsite(rs.getString("website"));}catch(Exception ex){}
			try{userInfo.setOther(rs.getString("other"));}catch(Exception ex){}
			try{userInfo.setSign(rs.getString("sign"));}catch(Exception ex){}
			try{userInfo.setPostsCounts(rs.getLong("postsCounts"));}catch(Exception ex){}
			userInfo.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UserInfo> userInfoCollection) throws SQLException {
		while(rs.next()){
			UserInfo userInfo=new UserInfo();
			try{userInfo.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userInfo.setNicename(rs.getString("nicename"));}catch(Exception ex){}
			try{userInfo.setIcon(rs.getLong("icon"));}catch(Exception ex){}
			try{userInfo.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{userInfo.setFax(rs.getString("fax"));}catch(Exception ex){}
			try{userInfo.setWebsite(rs.getString("website"));}catch(Exception ex){}
			try{userInfo.setOther(rs.getString("other"));}catch(Exception ex){}
			try{userInfo.setSign(rs.getString("sign"));}catch(Exception ex){}
			try{userInfo.setPostsCounts(rs.getLong("postsCounts"));}catch(Exception ex){}
			userInfo.resetModified();
			userInfoCollection.add(userInfo);
		}
	}
	public static boolean update(UserInfo userInfo, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UserInfoPeer.columnNames.length; i++) {
			if (((UserInfoBase)userInfo).modified[i]) {
				columns.add(UserInfoPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `UserInfo` SET ");
		sql.append("WHERE TRUE AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,userInfo.get(column));
		}
		pstmt.setObject(columns.size()+1+0,userInfo.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UserInfo userInfo, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (userInfo.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `UserInfo` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (userInfo.get(column) != null) {
				pstmt.setObject(i + 1 + j, userInfo.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UserInfo> getUserInfoCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UserInfo> collection=new Vector<UserInfo>();
		fill(rs,collection);
		return collection;
	}
}
